package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilterResumeDescription {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-resumeDescription.csv";
        String mothodname = "resumeDescription";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,4);
    }
    @Test(description = "描述信息中包含敏感词、违禁词共有5个用户，其中544234781为正常用户，其他描述包含敏感词；code状态码是否为0，msg信息是否为success")
    public void checkCusrIdNameContactSensitiveWord(){
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        //校验每个参数
        List<String> diffUserid = new ArrayList<String>(cuIds);
        for (String userid: cuIds ) {
            for (Object aResurlt : resurlt) {
                if (Objects.equals(String.valueOf(aResurlt),String.valueOf(userid))) {
                    diffUserid.remove(userid);
                    break;
                }
            }
        }
        Reporter.log("有被过滤掉的数据，具体的userid:"+diffUserid);
        Assert.assertEquals(4,diffUserid.size());
        Assert.assertEquals(1,resurlt.size());
        Assert.assertEquals(String.valueOf(544234781),String.valueOf(resurlt.get(0)));
        Assert.assertEquals(msg,"success");
    }

    @Test(description = "建立描述中只有一个用户534234783，并且该用户信命中包含敏感词，预期返回结果为空")
    public void checkOnlyoneCusrIdNameContactSensitiveWord(){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("cuIds","544234779");
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "姓名敏感词校验，其中两个用户544234778,544234779都命中铭感次，预期返回结果为空")
    public void checkTwoCusrIdNameContactSensitiveWord(){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        String cuIds="[534234786,534234788]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "姓名敏感词校验，544234780包含违禁词，544234781为正常用户，预期返回526381799")
    public void checkCusrIdNameContactBannedWord(){
        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        String cuIds="[544234780,544234781]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),1);
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(544234781));
        Assert.assertEquals(code,"200");
    }
    //// TODO: 2019/11/26 负向标签、电话拨通五次、虚拟号码
    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
    }
}
